﻿using Application.Dto;
using Application.IAppSrevices;
using Domain.Entitys;
using Microsoft.AspNetCore.Mvc;
using System.Net.Mail;
using System.Net;
using UI.Hubs;
using Microsoft.AspNetCore.SignalR;

namespace UI.Controllers
{
    public class ERController : Controller
	{
		private readonly ICMService cMService;
		private readonly IEMRService eMRService;
		private readonly IERService eRService;
		private readonly IEIService eIService;
        private readonly IHubContext<ChatHub> hubContext;

        public ERController(ICMService cMService, IEMRService eMRService, IERService eRService, IEIService eIService,IHubContext<ChatHub> hubContext)
		{
			this.cMService = cMService;
			this.eMRService = eMRService;
			this.eRService = eRService;
			this.eIService = eIService;
            this.hubContext = hubContext;
        }
		public IActionResult Index(int id)
		{
			TempData["zwfl"] = id;
			//ViewData.Model = await cMService.CMSelectName(name);
			return View();
		}
		public IActionResult IndexYXJLCX(int id)
		{

			return View();
		}
		public IActionResult IndexMSSX(int id)
		{

			return View();
		}
		public IActionResult IndexJLCX(int id)
		{

			return View();
		}
		public IActionResult IndexJLCXFH(int id)
		{
			string userName = HttpContext.Session.GetString("UTrueName");
			ViewBag.UserTrueName = userName;
			TempData["id"] = id;
			return View();
		}
		public IActionResult IndexYXJLCXFH(int id)
		{
			string userName = HttpContext.Session.GetString("UTrueName");
			ViewBag.UserTrueName = userName;
			TempData["id"] = id;
			return View();
		}

		public IActionResult IndexMSDJEI(int id)
		{
			string userName = HttpContext.Session.GetString("UTrueName");
			ViewBag.UserTrueName = userName;
			TempData["id"] = id;
			return View();
		}

		public IActionResult IndexMSUpdateEI(int id)
		{
			string userName = HttpContext.Session.GetString("UTrueName");
			ViewBag.UserTrueName = userName;
			TempData["id"] = id;
			return View();
		}

		public IActionResult IndexLYSQxg(int id)
		{
			string userName = HttpContext.Session.GetString("UTrueName");
			ViewBag.UserTrueName = userName;
			TempData["id"] = id;
			return View();
		}

		public IActionResult IndexLYSQ1xg(int id)
		{
			string userName = HttpContext.Session.GetString("UTrueName");
			ViewBag.UserTrueName = userName;
			TempData["id"] = id;
			return View();
		}

		public IActionResult IndexLYCX1(int id)
		{
			string userName = HttpContext.Session.GetString("UTrueName");
			ViewBag.UserTrueName = userName;
			TempData["id"] = id;
			return View();
		}




		public IActionResult IndexMSJGDJ()
		{
			return View();
		}

		public IActionResult IndexLYSQ()
		{
			return View();
		}
		public IActionResult IndexLYSQ1()
		{
			return View();
		}
		public IActionResult IndexLYCX()
		{
			return View();
		}

		public async Task<EMR> EeSelect()
		{
			int name1 = (int)TempData["zwfl"];
			Console.WriteLine(name1);

			return await eMRService.EMRSelectId(name1);

		}

        [TypeFilter(typeof(SingarFilterAttribute), Arguments = new object[] { "招聘管理--简历管理" })]
        public async Task<int> ERAdd(ERAddDto eRAddDto)
		{
			return await eRService.ERAddAsync(eRAddDto);
		}
		//长富的代码
		[HttpPost]
        public async Task<IActionResult> UploadAvatar(IFormFile avatar)
        {
            if (avatar == null || avatar.Length == 0)
                return BadRequest("No file uploaded.");

            var uploadDir = @"D:\圈圈\mimi\UI\wwwroot\Img\";

            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            // 生成新的文件名
            var random = new Random();
            var randomNumber = random.Next(100, 1000); // 生成三位随机数
            var newFileName = $"{DateTime.Now:yyyyMMddHHmmssfff}{randomNumber}{Path.GetExtension(avatar.FileName)}";

            var filePath = Path.Combine(uploadDir, newFileName);

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                await avatar.CopyToAsync(fileStream);
            }

            // 返回文件的相对路径给前端
            var relativePath = "/Img/" + newFileName;
            return Ok(new { filePath = relativePath });
        }

        public async Task<List<ER>> ERSelectSie(int flid, int ncid, string name, DateTime time, DateTime timimo, string CheckStatus, int pageIndex, int pageSize)
		{
			List<ER> erselect = await eRService.ERSelectSie(flid, ncid, name, time, timimo, CheckStatus, pageIndex, pageSize);
			return await eRService.ERSelectSie(flid, ncid, name, time, timimo, CheckStatus, pageIndex, pageSize);
		}
		public IActionResult ERSelectCount()
		{
			int num = eRService.ERSelectCount();
			return Json(num);
		}

		public async Task<IActionResult> SelectId()
		{
			int id = (int)TempData["id"];
			ER er = await eRService.SelectId(id);
			return Json(er);
		}
		public async Task<int> ERUpdate(ERupdateDto eRupdateDto)
		{
			return await eRService.ERUpdateAsync(eRupdateDto);
		}

		public async Task<List<ER>> SelectZTid(int id, int pageIndex, int pageSize)
		{
			List<ER> erselect = await eRService.SelectZTId(id, pageIndex, pageSize);
			return await eRService.SelectZTId(id, pageIndex, pageSize);
		}
		public async Task<List<EI>> EISelect(string name)
		{
			return await eIService.EISelectID(name);
		}

        [TypeFilter(typeof(SingarFilterAttribute), Arguments = new object[] { "招聘管理--面试管理" })]
        public async Task<IActionResult> EIAdd(EI eI)
		{
			int num = await eIService.EIAdd(eI);
			return Json(num);
		}

		public async Task<int> ERUpdateZTid(ERUpdateZTDto eRUpdateZTDto)
		{
			return await eRService.ERUpdateZTid(eRUpdateZTDto);
		}

		public async Task<List<ER>> SelectMSZTId(int id, int pageIndex, int pageSize)
		{
			List<ER> erselect = await eRService.SelectMSZTId(id, pageIndex, pageSize);
			return await eRService.SelectMSZTId(id, pageIndex, pageSize);
		}

		public async Task<int> ERUpdateSXID(ERUpdateSXDto eRUpdateSXDto)
		{
			if (eRUpdateSXDto.PassCheckComment == "通过")
			{
                await hubContext.Clients.All.SendAsync("ReceiveMessage", "招聘管理--录用管理", "新增1条审核信息");
            }
			return await eRService.ERUpdateSXid(eRUpdateSXDto);
		}

		public async Task<int> EIUpdateSX(EIUpdateSXDto eIUpdateSXDto)
		{
			return await eIService.EIUpdateSX(eIUpdateSXDto);
		}

		public async Task<int> ERUpdateSX2id(ERUpdateSX2Dto eRUpdateSX2Dto)
		{
			return await eRService.ERUpdateSX2id(eRUpdateSX2Dto);
		}

		public async Task<List<ER>> SelectLYCX(string PassCheckComment, string PassPassComment, int pageIndex, int pageSize)
		{
			List<ER> er = await eRService.SelectLYCX(PassCheckComment, PassPassComment, pageIndex, pageSize);
			return await eRService.SelectLYCX(PassCheckComment, PassPassComment, pageIndex, pageSize);
		}

		public async Task<List<ER>> SelectLYSQ(string PassCheckComment, int pageIndex, int pageSize)
		{
			List<ER> er = await eRService.SelectLYSQ(PassCheckComment, pageIndex, pageSize);
			return await eRService.SelectLYSQ(PassCheckComment, pageIndex, pageSize);
		}

        public int ERSelectIDCountxs(int id)
        {
            return eRService.ERSelectIDCount(id);
        }

        public int ERSelectString2Count(string PassCheckComment, string PassPassComment)
        {
            return  eRService.ERSelectString2Count(PassCheckComment, PassPassComment);
        }
        public  int ERSelectStringCount(string PassCheckComment)
        {
            return eRService.ERSelectStringCount(PassCheckComment);
        }

		public int ERSelectCount77(int flid, int ncid, string name, DateTime time, DateTime timimo, string CheckStatus)
		{
			return eRService.ERSelectCount(flid, ncid, name, time, timimo, CheckStatus);
		}
		public async Task<IActionResult> SelectMSZTIdCount(int id)
		{
			return Json(await eRService.SelectMSZTIdCount(id));
		}

		/// <summary>
		/// 发送邮件方法
		/// </summary>
		/// <param name="mailTo">接收人邮件</param>
		/// <param name="mailTitle">发送邮件标题</param>
		/// <param name="mailContent">发送邮件内容</param>
		/// <returns></returns>
		[HttpGet("ER/SendEmail")]
		public void SendEmail(string mailTo, string mailTitle, string mailContent)
        {
            var smtpClient = new SmtpClient
            {
                Host = "smtp.qq.com", // QQ邮箱的SMTP服务器地址
                Port = 587,           // SMTP服务器的端口，QQ邮箱使用587
                EnableSsl = true,     // 启用SSL
                Credentials = new NetworkCredential("2234744221@qq.com", "atercbrlroaxdibc") // 使用QQ邮箱和授权码
            };
            // 设置邮件内容为HTML模板
            string htmlContent = @"
				<!DOCTYPE html>
				<html lang=""zh"">
				<head>
					<meta charset=""UTF-8"">
					<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
					<title>恭喜您被录取</title>
					<style>
						body {
					font-family: Arial, sans-serif;
					background-color: #f4f4f4;
					color: #333;
					padding: 20px;
				}
				.container {
					background-color: #fff;
					max-width: 600px;
					margin: 0 auto;
					padding: 20px;
					box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
				}
				.header {
					text-align: center;
					padding: 20px 0;
				}
				.header img {
					max-width: 100%;
				}
				.content {
					padding: 20px 0;
				}
				.footer {
					text-align: center;
					padding: 20px 0;
					background-color: #f9f9f9;
					margin-top: 20px;
				}
					</style>
				</head>
				<body>
					<div class=""container"">
				<div class=""header"">
					<h1>恭喜您，[姓名]！</h1>
					<p>您已被我们的公司录取。</p>
				</div>
				<div class=""content"">
					<p>我们非常高兴地通知您，经过我们团队的慎重考虑，您已被我们的公司录取。</p>
					<p>我们相信您的加入将为我们的团队带来新的活力和创新。</p>
					<p>我们的HR部门将会在近期与您联系，安排入职事宜。</p>
					<p>再次祝贺您，并期待与您一起共创美好未来！</p>
				</div>
				<div class=""footer"">
					<p>此致</p>
					<p>[深圳市腾讯计算机系统有限公司]</p>
				</div>
			</div>
				</body>
				</html>
			";
            htmlContent = htmlContent.Replace("[姓名]", mailContent);
            // 创建邮件消息
            var mailMessage = new MailMessage
            {
				//发送邮件账号
                From = new MailAddress("2234744221@qq.com"),
                Subject = mailTitle,
                Body = htmlContent,
                IsBodyHtml = true // 如果邮件内容是HTML格式
            };

            mailMessage.To.Add(mailTo);
            try
            {
                // 发送邮件
                smtpClient.Send(mailMessage);
            }
            catch (SmtpException ex)
            {
                // 异常处理
                Console.WriteLine("发送邮件时出错: " + ex.Message);
            }
        }
    }
}
